package xyz.qduwo.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import xyz.qduwo.entity.*;
import xyz.qduwo.util.HibernateUtils;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.query.Query;

public class RoomDao {

    /**
     * 保存一个房间
     * @param roomEntity
     */
    public static void save(RoomEntity roomEntity) {
        Session session = HibernateUtils.openSession();
        try {
            Transaction tx = session.beginTransaction(); // 开启事务
            session.save(roomEntity);
            tx.commit(); // 提交事务
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); // 回滚事务
            throw e;
        } finally {
            session.close(); // 关闭session
        }
    }
    
    /**
    *
    * @param
    */
    public static RoomEntity getroom(String num){
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("FROM RoomEntity WHERE  num='"+num+"'and endTime is NULL ");
            RoomEntity room = (RoomEntity) query.uniqueResult();// 操作
            tx.commit();
            return room;
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
    /**
     * 更新一个房间
     * @param roomEntity
     */
    public static void update(RoomEntity roomEntity) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            session.update(roomEntity);// 操作

            tx.commit();
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }


    /**
     * 删除画布作者
     * @param roomEntity
     */
    public static void delete(RoomEntity roomEntity) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.delete(roomEntity); // 删除的是实体对象

            tx.commit();
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }


    /**
     * 根据房间对像查询用户列表
     * @param roomEntity
     * @return
     */
    public static List<UserEntity> listuser(RoomEntity roomEntity) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            List<RoomUserlistEntity> RoomUserlist =session.createQuery("FROM RoomUserlistEntity WHERE room='"+roomEntity.getId()+"'").list(); // 使用HQL查询
            List<UserEntity> userlist = new ArrayList<UserEntity>();
            for(RoomUserlistEntity roomUserlistEntity:RoomUserlist)
                userlist.add(new UserDao().getByUserID(roomUserlistEntity.getUser()));
            tx.commit();
            return userlist;
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

    /**
     * 分页的查询数据列表
     * @param firstResult 从结果列表中的哪个索引开始取数据
     * @param maxResults 最多取多少条数据
     * @return 一页的数据列表
     */
    @SuppressWarnings("unchecked")
    public static QueryResult findAll(int firstResult, int maxResults) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            // 查询一页的数据列表
            // 方式一：
            // Query query = session.createQuery("FROM CanvasEntity");
            // query.setFirstResult(firstResult);
            // query.setMaxResults(maxResults);
            // List<CanvasEntity> list = query.list(); // 使用HQL查询

            // 方式二：方法链
            List<CanvasEntity> list = session.createQuery( //
                    "FROM CanvasEntity") //
                    .setFirstResult(firstResult) //
                    .setMaxResults(maxResults) //
                    .list();

            // 查询总记录数
            // session.createQuery("SELECT COUNT(*) FROM CanvasEntity").list().get(0);
            // Long count = (Long) session.createQuery("SELECT COUNT(*) FROM CanvasEntity").uniqueResult();
            Long count = (Long) session.createQuery( //
                    "SELECT COUNT(*) FROM CanvasEntity") //
                    .uniqueResult();
            tx.commit();

            // 返回结果
            return new QueryResult(count.intValue(), list);
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
}
